<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {ajax_link} function plugin
 *
 * Type:     function<br>
 * Name:     Include_js<br>
 * Purpose:  Generates Java Script for AJAX request
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_ajax_link($params, &$smarty)
{
	
	$update = null;
	$method = 'post';
	$asynchronous = 'true';
	$evalScripts = 'true';
	$vars = null;
	$return = true;
	
	if ($params['loader']) 
	{
		$onLoading = "Element.show_inline('" . $params['loader'] . "')";
		$onComplete = "Element.hide('" . $params['loader'] . "')";
	}
	elseif ($smarty->ajax_loader_element) 
	{
		$onLoading = "Element.show('" . $smarty->ajax_loader_element . "')";
		$onComplete = "Element.hide('" . $smarty->ajax_loader_element . "')";
		
	}
	
	foreach ($params as $_key => $_value) {
		
		switch ($_key) {
			case 'url':
			case 'update':
			case 'method':
			case 'vars':
			case 'jsvars':
			case 'self_value':
			case 'onSuccess':
			case 'return':
				$$_key = $_value;
				break;
			
		}
		
	}
	
	$url = $url ? "'" . $url . "'"  : 'this.href';
	
	$_html_result = "new Ajax.Updater(
										{
											success :'$update'
										},
										$url,
											{
												asynchronous:	$asynchronous,
												method:			'$method', 
												evalScripts:	$evalScripts ";
	if ($onComplete) $_html_result .=			", onComplete:		function(request) { $onComplete } ";
	if ($onLoading)  $_html_result .=			", onLoading:		function(request) { $onLoading } ";
	if ($onSuccess)  $_html_result .=			", onSuccess:		function(request) { $onSuccess } ";

	if ($vars) $_html_result .=					", parameters:		'$vars'";
	if ($jsvars) $_html_result .=				", parameters:		" . $jsvars;
	if ($self_value) $_html_result .=			", parameters:		'$self_value='+this.value";
	
	$_html_result .= 						"}
					);";
	if($return) $_html_result .= "return false;";
	
    return $_html_result;

}


/* vim: set expandtab: */

?>
